package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("RoleController+查询所有角色数据");
        List<Role> roleList = roleService.findAll();
        log.debug("RoleController+查询到的所有角色数据" + roleList.toString());
        return new Result(true,"查询角色数据成功",roleList);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.debug("RoleController-findPage-分页展示: QueryPageBean{ " + pageBean + " }");
        PageResult pageResult = roleService.findPage(pageBean);
        log.debug("RoleController-findPage-分页展示: PageResult {" + pageResult + " }");
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] menuIds){
        log.debug("RoleController-add:role新增角色：" + role);
        log.debug("RoleController-add:menuIds关联的数组：" + Arrays.toString(menuIds));
        roleService.add(role,menuIds);
        log.debug("新增角色成功！！");
        return new Result(true, "新增角色成功！！");
    }

    //根据id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("RoleController-findById:要查询的id：" + id);
        Role role = roleService.findById(id);
        log.debug("RoleController-findById:根据id查询的角色：" + role);
        return new Result(true,"查询角色成功！",role);
    }

    //根据id删除角色
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("RoleController-delById 要删除的角色id: { " + id +"}" );
        roleService.delRoleById(id);
        log.debug("删除角色成功");
        return new Result(true,"删除角色成功");
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] menuIds){
        log.debug("RoleController-edit 要编辑的角色: { " + role +"}" );
        log.debug("RoleController-delById 关联的菜单数组: { " + Arrays.toString(menuIds) +"}" );
        roleService.edit(role,menuIds);
        log.debug("修改角色成功！");
        return new Result(true, "修改角色成功！");
    }

    //根据角色id查询菜单项信息
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer roleId){
        log.debug("RoleController-findMenuIdsByRoleId:要查询的角色id { " + roleId +"}" );
        List<Integer> menuIdsList = roleService.findMenuIdsByRoleId(roleId);
        log.debug("RoleController-findMenuIdsByRoleId:要查询的角色id对应的菜单项id： { " + menuIdsList +"}" );
        return new Result(true,"根据角色id查询菜单项成功",menuIdsList);
    }
}
